import ctypes
import threading
import time

from src import enum

gm = ctypes.CDLL(r'./lib/ghub_device.dll')
driver = ctypes.CDLL(r'./lib/MouseControl.dll')

shoting = False
zhuanging = False

def click():
    global shoting
    if shoting:
        return
    def do_something():
        global shoting
        if shoting:
            return
        shoting = True

        if enum.setting['driver'] == "MouseControl":
            driver.click_Left_down()
            time.sleep(0.05)
            driver.click_Left_up()

        elif enum.setting['driver'] == "ghub_device":
            gm.mouse_down(1)
            time.sleep(0.05)
            gm.mouse_up(1)

        shoting = False
    t = threading.Thread(target=do_something)
    t.start()

def shot(target, shot_point):

    mx = (target[0]-shot_point[0])*enum.setting['x_offset']
    yoffset = list(map(float, enum.setting['y_offset'].split(',')))
    my = (target[1]-shot_point[1] - shot_point[2]*yoffset[1])*yoffset[0]

    print("自动校准后", mx, my)
    global shoting
    if shoting:
        return

    def do_something():

        global shoting
        if shoting:
            return
        shoting = True
        print(enum.setting['driver'])
        if enum.setting['driver'] == "MouseControl":
            driver.move_R(int(mx), int(my))

            driver.click_Left_down()
            time.sleep(0.05)
            driver.click_Left_up()

        elif enum.setting['driver'] == "ghub_device":
            gm.moveR(int(mx), int(my))
            gm.mouse_down(1)
            time.sleep(0.05)
            gm.mouse_up(1)

        shoting = False

    #开启一个新线程，执行do_something函数
    t = threading.Thread(target=do_something)
    t.start()


def zhuang():
    global zhuanging
    if zhuanging:
        return

    def do_something():
        global zhuanging
        if zhuanging:
            return

        zhuanging = True

        if enum.setting['driver'] == "MouseControl":
            driver.click_Right_down()
            time.sleep(0.05)
            driver.click_Right_up()

        elif enum.setting['driver'] == "ghub_device":
            gm.mouse_down(2)
            time.sleep(0.05)
            gm.mouse_up(2)

        zhuanging = False

    # 开启一个新线程，执行do_something函数
    t = threading.Thread(target=do_something)
    t.start()

